var app = getApp();
Page({
    data: {
        'text': '',
        msgData: [],

        star: 0,
        starMap: [
            '一星',
            '两星',
            '三星',
            '四星',
            '五星',
        ],
        oNumber: '',
        eContent: ''
    },
    changeInputValue(ev) {
        this.setData({
            eContent: ev.detail.value,
        })
    },
    //删除留言
    DelMsg(ev) {
        var that = this
        wx.showModal({
            title: '提示',
            content: '确定删除此条评论？',
            success: function(e) {
                if (e.confirm) {
                    var n = ev.target.dataset.index;
                    var list = that.data.msgData;
                    list.splice(n, 1);
                    that.setData({
                        msgData: list
                    });
                } else {}
            }

        })
    },

    //添加留言
    addComment: function(e) {
        var that = this;
        var oNumber = e.currentTarget.dataset.onumber;
        var pid = e.currentTarget.dataset.pid;
        console.log("oNumber1111" + oNumber);
        //判断输入框为空
        var input = that.data.eContent; // 获取评论内容
        if (input.length > 5 && input.length < 80) {
            // var list = this.data.msgData;
            // list.push({
            //   msg: this.data.inputVal
            // });
            if (this.data.star === 0) {
                wx.showToast({
                    title: '请选择评分',
                    icon: 'none',
                    duration: 2000
                });
                return
            }
            wx.request({
                url: 'https://www.brvpmusic.com/api/userEvaluation/addEvaluation',
                // url: 'http://192.168.0.107:8080/api/userEvaluation/addEvaluation',
                method: 'GET',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                    oNumber: oNumber,
                    pPid: pid,
                    openid: wx.getStorageSync('OpenId'),
                    eContent: that.data.eContent,
                    score: that.data.star
                },
                success: function(res) {
                    console.log(res.data);
                    if(res.data.code==200){
                        console.log('评价成功');
                        wx.showToast({
                            title: '评价成功',
                        })
                        setTimeout(() => {
                            wx.redirectTo({
                                url: "/pages/goods-details/index?id=" + pid
                            })
                        }, 1000);
                    }else{
                        wx.showToast({
                            title: res.data.message,
                            icon:"none"
                        });
                    }


                }
            })
        } else {
            if (input.length > 80) {
                wx.showToast({
                    title: '评论字数超出限制!',
                    icon: 'none',
                    duration: 2000
                });
            } else {
                wx.showToast({
                    title: '评论字数不够!',
                    icon: 'none',
                    duration: 2000
                });
            }
            return false;
        }
    },
    myStarChoose(e) {
        let star = parseInt(e.target.dataset.star) || 0;
        this.setData({
            star: star,
        });
    },
    getcomment: function(pid) {
        var that = this
        wx.showLoading({
            title: '加载中...',
        })
        var parm = pid;
        console.log("parm" + parm);
        wx.request({
            url: 'https://www.brvpmusic.com/api/userEvaluation/EvaluationList',
            // url: 'http://3s.dkys.org:12457/api/userEvaluation/EvaluationList',
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                pPid: parm, //string 
            },
            success: function(res) {
                console.log(res.data);
                that.setData({
                    msgData: res.data.body
                })

            },
            complete() {
                wx.hideLoading()
            }
        })
    },
    onShow: function(e) {},

    onLoad: function(e) {
        this.getcomment(e.pid);
        console.log("onumber---" + e.onumber);
        console.log(e.pid);
        this.setData({
            oNumber: e.onumber,
            pid: e.pid
        })
    }
})